package cn.aj.org.controller;


import cn.aj.basic.utils.AjaxResultUtil;
import cn.aj.basic.utils.PageListUtil;
import cn.aj.org.domain.Employee;
import cn.aj.org.service.EmployeeService;
import cn.aj.org.query.EmployeeQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的api", description = "员工的crud以及其它功能")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    
    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/13 0013 23:25  
     * 查询全部
    */
    @GetMapping
    @ApiOperation(value = "员工查询全部")
    public List<Employee> loadAll() {
        return employeeService.loadAll();
    }

    @PostMapping
    @ApiOperation(value = "员工分页查询")
    public PageListUtil<Employee> loadPage(@ApiParam(value="员工封装的每页条数以及页码",required = true) @RequestBody EmployeeQuery employeeQuery) {
        PageListUtil<Employee> list = employeeService.loadPage(employeeQuery);
        return list;
    }
    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/13 0013 23:25  
     * 根据id查询
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "员工根据id查询")
    public Employee getById(@ApiParam(value="员工id",required = true) @PathVariable("id") Long id) {
        return employeeService.getById(id);
    }


   /**
   * @Description: TODO
   * @Author: aj
   * @Version: 1.0
   * @Date:  2022/11/13 0013 23:26  
    * 删除
   */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "员工删除")
    public AjaxResultUtil delete(@ApiParam(value="员工id",required = true) @PathVariable("id") Long id) {
        employeeService.delete(id);
        return AjaxResultUtil.getAjaxResult().success();
    }

   /**
   * @Description: TODO
   * @Author: aj
   * @Version: 1.0
   * @Date:  2022/11/13 0013 23:26  
    * 新增或者修改
   */
    @PutMapping
    @ApiOperation(value = "员工新增或者修改")
    public AjaxResultUtil addOrUpdate(@ApiParam(value="员工实体类",required = true) @RequestBody Employee employee) {
        if (employee.getId() == null) { // 新增
            employeeService.add(employee);
        } else { // 修改
            employeeService.update(employee);
        }
        return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Description: TODO
    * @Author: aj
    * @Version: 1.0
    * @Date:  2022/11/13 0013 23:26
     * 批量删除
    */
    @PatchMapping
    @ApiOperation(value = "员工批量删除")
    public AjaxResultUtil batchDelete(@ApiParam(value="员工id",required = true) @RequestBody List<Long> ids) {
        employeeService.batchDelete(ids);
        return AjaxResultUtil.getAjaxResult().success();
    }
}
